package util.List;

/**
 * @Description
 * @Author Firenut
 * @Date 2023-07-19 00:40
 */
public class ListNode {
    public int val;        //默认值为0
    public ListNode next;  //默认值为null
    public ListNode() {}
    public ListNode(int val) { this.val = val; }
    public ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    // 打印链表的通用方法
    public static void printLinkedList(ListNode head) {
        while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }
    }

    // 创建链表的通用方法
    public static ListNode createLinkedList(int[] values) {
        if (values == null || values.length == 0) {
            return null; // 返回空链表
        }

        ListNode head = new ListNode(values[0]); // 创建头节点
        ListNode current = head; // 创建一个当前节点指针，初始指向头节点

        // 从第二个元素开始遍历数组，逐个创建链表节点并连接起来
        for (int i = 1; i < values.length; i++) {
            current.next = new ListNode(values[i]);
            current = current.next;
        }
        current.next = null;
        return head; // 返回链表头节点
    }
}
